package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.vo.legaldocument.LegalDocumentCreateVo;
import org.summer.lawyer.domain.vo.legaldocument.LegalDocumentUpdateVo;
import org.summer.lawyer.domain.vo.legaldocument.LegalDocumentVo;
import org.summer.lawyer.service.ILegalDocumentService;

import java.util.List;

/**
 * 用户协议
 *
 * @author summer
 * @date 2024-06-16
 */
@Tag(name = "后台相关接口", description = "后台用户协议相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/doc")
public class LegalDocumentController extends BaseController {

    private final ILegalDocumentService legalDocumentService;

    /**
     * 查询用户协议列表
     */
    @Operation(summary = "查询用户协议列表", description = "查询用户协议列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:document:list")
    @GetMapping("/list")
    public TableDataInfo<LegalDocumentVo> list(LegalDocumentVo vo, PageQuery pageQuery) {
        return legalDocumentService.selectPageList(vo, pageQuery);
    }

    /**
     * 获取用户协议详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "查询用户协议列表", description = "查询用户协议列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:document:query")
    @GetMapping("/{id}")
    public R<LegalDocumentVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(legalDocumentService.selectLegalDocumentVoById(id));
    }

    /**
     * 新增用户协议
     */
    @Operation(summary = "新增用户协议", description = "新增用户协议", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:document:add")
    @Log(title = "用户协议", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveDoc")
    public R<Void> saveLegalDocument(@Validated(AddGroup.class) @RequestBody LegalDocumentCreateVo vo) {
        LegalDocumentVo legalDocumentVo = new LegalDocumentVo();
        legalDocumentVo.setVersion(vo.getVersion());
        legalDocumentVo.setDocType(vo.getDocType());
        if(legalDocumentService.checkVersionUnique(legalDocumentVo)){
            throw new ServiceException(StrUtil.format("版本号“{}”已经存在,请重新输入", vo.getVersion()));
        }
        return toAjax(legalDocumentService.insertByVo(vo));
    }

    /**
     * 修改用户协议
     */
    @Operation(summary = "修改用户协议", description = "修改用户协议", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:document:edit")
    @Log(title = "用户协议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateDoc")
    public R<Void> updateLegalDocument(@Validated(EditGroup.class) @RequestBody LegalDocumentUpdateVo vo) {
        LegalDocumentVo legalDocumentVo = new LegalDocumentVo();
        legalDocumentVo.setVersion(vo.getVersion());
        legalDocumentVo.setId(vo.getId());
        legalDocumentVo.setDocType(vo.getDocType());
        if(legalDocumentService.checkVersionUnique(legalDocumentVo)){
            throw new ServiceException(StrUtil.format("版本号“{}”已经存在,请重新输入", vo.getVersion()));
        }
        return toAjax(legalDocumentService.updateByVo(vo));
    }

    /**
     * 删除用户协议
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除用户协议", description = "删除用户协议", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:document:remove")
    @Log(title = "用户协议", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeDoc/{ids}")
    public R<Void> removeLegalDocument(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(legalDocumentService.deleteWithValidByIds(List.of(ids), true));
    }
}
